package cn.com.xjnt.vas.web.ordering.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.frameworks.core.web.controller.BaseController;
import com.frameworks.core.web.result.DataResult;

import cn.com.xjnt.vas.ordering.domain.entity.Department;
import cn.com.xjnt.vas.ordering.domain.entity.Ward;
import cn.com.xjnt.vas.ordering.service.DepartmentService;
import cn.com.xjnt.vas.ordering.service.WardService;

/** 
* 类说明：对外接口：智能卡信息查询
* @author 作者 LUXIN
* @version 创建时间：2016年5月25日 上午11:34:50 
*/
@Controller
@RequestMapping("/api/cardno")
public class CardnoAPI extends BaseController {
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private WardService wardService;
    
    @RequestMapping(value = "/get", method = { RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
    @ResponseBody
    public DataResult getByCardno(@RequestBody JSONObject json) {
        DataResult dataRet = new DataResult();
        try {
            JSONObject result = new JSONObject();
            String cardno = json.getString("cardno");
            Department department = departmentService.getByCardno(cardno);
            if(department == null){
                Ward ward = wardService.getByCardno(cardno);
                if(ward == null){
                    dataRet.pushError("智能卡未维护相关信息");
                }else{
                    result.put("name", ward.getName());
                    result.put("hospitalId", ward.getHospital().getId());
                    result.put("orgId", ward.getOrganization().getId());
                }
            }else{
                result.put("name", department.getName());
                result.put("hospitalId", department.getHospital().getId());
                result.put("orgId", department.getOrganization().getId());
            }
            dataRet.pushOk("获取智能卡地址信息成功");
            dataRet.setRows(result);
        } catch (Exception e) {
            dataRet.pushError("获取智能卡地址信息失败");
            logger.error("获取智能卡地址信息错误",e);
        }
        return dataRet;
    }
}
 